package com.jt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.service.ItemService;
import com.jt.vo.EasyUITable;
import com.jt.vo.SysResult;

@RestController
@RequestMapping("/item")
public class ItemController {
	
	@Autowired
	private ItemService itemService;
	
	//分页查询
	//请求路径：http://localhost:8091/item/query?page=1&rows=20
	@RequestMapping("/query")
	public EasyUITable findItemByPage(Integer page,Integer rows) {
		EasyUITable vo = itemService.findItemByPage(page, rows);
		return vo;
	}
	
	//商品数据和商品详情新增
	@RequestMapping("/save")
	public SysResult saveItem(Item item,ItemDesc itemDesc) {
		//有全局异常处理类后可以这样写
		itemService.saveItem(item,itemDesc);
		return SysResult.success();
	}
	
	//商品数据及商品详情修改
	@RequestMapping("/update")
	public SysResult updateItem(Item item,ItemDesc itemDesc) {
		itemService.updateItem(item,itemDesc);
		return SysResult.success();
	}
	
	//商品下架
	@RequestMapping("/instock")
	public SysResult instock(Long[] ids) {
		int status=2;//表示下架
		itemService.updateStatus(ids,status);
		return SysResult.success();
	}
	
	//商品上架
	@RequestMapping("/reshelf")
	public SysResult reshelf(Long[] ids) {
		int status=1;
		itemService.updateStatus(ids, status);
		return SysResult.success();
	}
	
	//商品删除
	@RequestMapping("/delete")
	public SysResult deleteItem(Long[] ids) {
		itemService.deleteItem(ids);
		return SysResult.success();
	}
	
	//商品详情数据回显，修改商品时先回显
	@RequestMapping("/query/item/desc/{itemId}")
	public SysResult findItemDescById(@PathVariable Long itemId) {
		ItemDesc itemDesc=itemService.findItemDescById(itemId);
		return SysResult.success(itemDesc);
	}
	
}
